<template>
  <el-card class="table_box">
    <el-table v-loading="tableLoading" :data="tableData" row-key="id" border style="margin: 10px 0; width: 100%">
      <el-table-column align="center" label="id" prop="id" />
      <el-table-column align="center" label="名称" prop="name" show-overflow-tooltip />
      <el-table-column align="center" label="权限值" prop="code" show-overflow-tooltip />
      <el-table-column align="center" label="更新时间" prop="updateTime" show-overflow-tooltip />
      <el-table-column align="center" label="操作" width="300px" class="btn_box">
        <template #="{ row }">
          <el-button type="primary" icon="user" size="small" :disabled="row.level == 4" @click="handleNew(row)"
            >添加菜单</el-button
          >
          <el-button type="success" icon="edit" size="small" :disabled="row.level == 1" @click="handleEdit(row)">编辑</el-button>

          <el-popconfirm :title="`确认删除：${row.name}？`" width="200" @confirm="handleDel(row.id)">
            <template #reference>
              <el-button icon="delete" type="danger" plain size="small" :disabled="row.level == 1">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      v-model="showDialog"
      :title="menuParams.id ? '编辑菜单' : '新增菜单'"
      width="40%"
      close-on-click-modal
      @close="handleClose"
    >
      <el-form :model="menuParams" ref="menuRef" :rules="menuRules">
        <el-form-item label="菜单名称：" prop="name">
          <el-input placeholder="请输入菜单名称" v-model="menuParams.name"></el-input>
        </el-form-item>
        <el-form-item label="权限值：" prop="code">
          <el-input placeholder="请输入权限值" v-model="menuParams.code"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="handleDialogConfirm"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { reqMenuList, reqDelMenu, reqAddOrUpdateMenu } from "@/api/acl/menu";
import { Premission, PremissionList, MenuParam, ResponseData } from "@/api/acl/menu/type";
import { ElMessage } from "element-plus";

let tableLoading = ref<boolean>(false); // 控制表格加载状态
let tableData = ref<PremissionList>([]); // 所有菜单数据

let showDialog = ref<boolean>(false); // 控制新增/编辑弹窗的显示
let menuRef = ref(); // 获取菜单输入实例
let menuParams = ref<MenuParam>({
  id: undefined,
  code: "",
  level: 0,
  name: "",
  pid: 0
}); // 菜单输入参数
const menuRules: any = {
  name: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
  code: [{ required: true, message: "请输入权限值", trigger: "blur" }]
}; // 加入表单验证
// ---------------------------------------//

onMounted(() => {
  getAllMenu();
});
// -------------获取/删除菜单--------------------------//
// 获取所有菜单 展示到表格中
const getAllMenu = async () => {
  tableLoading.value = true;
  const res = await reqMenuList();
  if (res.code === 200) {
    tableLoading.value = false;
    tableData.value = res.data;
  }
};
// 删除菜单
const handleDel = async (id: number) => {
  const res: ResponseData = await reqDelMenu(id);
  if (res.code === 200) {
    ElMessage({ type: "success", message: "删除成功" });
    getAllMenu();
  } else {
    ElMessage({ type: "error", message: res.message });
  }
};
// -------------新增/编辑菜单--------------------------//
// 打开新增弹窗
const handleNew = (row: Premission) => {
  showDialog.value = true;
  Object.assign(menuParams.value, {
    pid: row.id,
    level: row.level + 1
  });
};
// 打开编辑弹窗
const handleEdit = (row: Premission) => {
  showDialog.value = true;
  Object.assign(menuParams.value, row);
};
// 弹窗确认
const handleDialogConfirm = async () => {
  // console.log(menuParams.value);
  const res: ResponseData = await reqAddOrUpdateMenu(menuParams.value);
  if (res.code === 200) {
    ElMessage({ type: "success", message: "操作成功！" });
    showDialog.value = false;
    getAllMenu();
  } else {
    ElMessage({ type: "error", message: res.message });
  }
};
// 关闭弹窗 清空内容
const handleClose = () => {
  menuRef.value.clearValidate();
  Object.assign(menuParams.value, {
    id: undefined,
    code: "",
    level: 0,
    name: "",
    pid: 0
  });
};
</script>
<style scoped lang="scss"></style>
